package com.bqtt.tumor.demo.presentation.controller;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bqtt.tumor.demo.business.DeseaseService;
import com.bqtt.tumor.demo.business.CityService;
import com.bqtt.tumor.demo.business.CityService;
import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.entity.City;
import com.bqtt.tumor.demo.entity.City;
import com.bqtt.tumor.demo.entity.Desease;
import com.bqtt.tumor.demo.entity.Feature;
import com.bqtt.tumor.demo.entity.City;
import com.bqtt.tumor.demo.entity.SystemUser;

public class city {

	
	public static String queryCity( SystemUser user,  ModelMap model, City condtion,BindingResult bindingResult)
	{
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		if(condtion==null)
		{
			condtion = new City();
		}
		
		if(!model.containsAttribute("new_city"))
		     model.addAttribute("new_city", new City());		
		if(!model.containsAttribute("edit_city"))
		     model.addAttribute("edit_city", new City());	

		
		model.addAttribute("province_list",InlineTools.getProvinceList());
		
		model.addAttribute("historySearchCondition", condtion);
		
		model.addAttribute("condition", condtion); //保留上次查找的字段
		
	    model.addAttribute("city_list", CityService.searchByCondition(condtion, 20, 1));
		
	    model.addAttribute("isAddNewShow","false");
		
		return "admin/city";
	}
	
	 
	public static String addCity(  SystemUser user,  ModelMap model, City new_city,BindingResult bindingResult)
	{
		//session 变量 ——不用自动添加到Model
		//condition currentUser
		
		model.addAttribute("province_list",InlineTools.getProvinceList());
		
		City historySearchCondition = (City) model.get("historySearchCondition");
		if(historySearchCondition==null)
			historySearchCondition = new City();
		
		if(!model.containsAttribute("new_city"))
		     model.addAttribute("new_city", new City());		
		if(!model.containsAttribute("edit_city"))
		     model.addAttribute("edit_city", new City());
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			//出错的话，就保持弹出窗口，显示错误
			
			model.addAttribute("isAddNewShow","true");
			// 其他变量需要手动添加到Model
			model.addAttribute("city_list", CityService.searchByCondition(historySearchCondition, 20, 1));
			
			model.addAttribute("condition", historySearchCondition);
			
			return "admin/city";
		}
		
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("currentUser"))
			{
				model.addAttribute("currentUser", new SystemUser());
			}
			return "login";
		}
		

		boolean result = CityService.add(new_city);
		
		if(result)
		{
			model.addAttribute("new_city", new City());		
			model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
		}
		else
		{
			model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
		}
		
		// 其他变量需要手动添加到Model
		model.addAttribute("condition", historySearchCondition);
		model.addAttribute("city_list", CityService.searchByCondition(historySearchCondition, 20, 1));
	    model.addAttribute("isAddNewShow","false");

		return "admin/city";
	} 
	
	public static String editCity( SystemUser user,  ModelMap model, City edit_city,BindingResult bindingResult)
	{
		
		model.addAttribute("province_list",InlineTools.getProvinceList());

		City historySearchCondition = (City) model.get("historySearchCondition");
		if(historySearchCondition==null)
			historySearchCondition = new City();
		
		if(!model.containsAttribute("new_city"))
		     model.addAttribute("new_city", new City());		
		if(!model.containsAttribute("edit_city"))
		     model.addAttribute("edit_city", new City());
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			//出错的话，就保持弹出窗口，显示错误
			
			model.addAttribute("isAddNewShow","true");
			// 其他变量需要手动添加到Model
			model.addAttribute("city_list", CityService.searchByCondition(historySearchCondition, 20, 1,edit_city.getCity_def_id()));
			
			model.addAttribute("condition", historySearchCondition);
			
			return "admin/city";
		}
		
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("currentUser"))
			{
				model.addAttribute("currentUser", new SystemUser());
			}
			return "login";
		}
		

		boolean result = CityService.update(edit_city);
		
		if(result)
		{
			model.addAttribute("new_city", new City());		
			model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
		}
		else
		{
			model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
		}
		
		// 其他变量需要手动添加到Model
		model.addAttribute("condition", historySearchCondition);
		model.addAttribute("city_list", CityService.searchByCondition(historySearchCondition, 20, 1));
	    model.addAttribute("isAddNewShow","false");

		return "admin/city";
	}



}
